﻿<!DOCTYPE html>
<HTML>
<HEAD>
<!--数据目录权限配置 团队赋予数据库权限-->
<TITLE>数据目录权限控制</TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link href="${mvcPath}/dacp-lib/bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link href="${mvcPath}/dacp-res/css/bootstrap-cover.css" type="text/css" rel="stylesheet" />
<link href="${mvcPath}/dacp-lib/ztree/zTreeStyle.css" type="text/css" rel="stylesheet" >
<script src="${mvcPath}/dacp-lib/jquery/jquery.min.js" type="text/javascript"></script>
<script src="${mvcPath}/dacp-lib/ztree/jquery.ztree.all-3.5.min.js"></script>

<script src="${mvcPath}/dacp-lib/cryptojs/aes.js"></script>
<script src="${mvcPath}/crypto/crypto-context.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.core.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.jsonstore.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.field.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.grid.js"></script>
<style>
	body {
	    background-color: #f2f4f8;
	}
	div.pageContainer {
		text-align: center;
	}
	
	
	div.pageContainer .pageSizeInput {
		height: 32px;
		width: 40px;
	}
	
	div.pageContainer .pageSizeButton {
		height: 32px;
		width:32px;
		position: relative;
		top: 0px;
	}
	.panel .table td,.panel .table th {
		padding: 8px 15px;
		border-top: 1px solid #eaeef1;
	}
</style>	
<SCRIPT type="text/javascript">
$(document).ready(function(){
	var code;
	var currentTeamCode="T0001";
	var setting = {
		view: {selectedMulti: false},
		check: {enable: true},
		data: {simpleData: {enable: true}}
	};

	var teamPermission="SELECT A.DBNAME id, 'root' pId, A.cnname NAME,CASE WHEN b.dbname IS NULL THEN 0 ELSE 1 END CHECKED FROM metadb_list A LEFT JOIN metadb_list_permission B ON A.dbname=B.dbname AND B.TEAM_CODE='{TEAM_CODE}'ORDER BY A.dbname";
	
	var ds = new AI.JsonStore({
		sql : teamPermission.replace('{TEAM_CODE}',currentTeamCode),
		pageSize : -1
	});
	var buildZTree = function(models){
		var zNodesNew = [];
		if(models&&models.length>1){
			for(var i=0;i<models.length;i++){
				var val = models[i];
				zNodesNew.push({id:val.ID,pId:val.PID,name:val.NAME,checked:val.CHECKED==1?true:false});
			}
			$("#treeDemo").empty();
			$.fn.zTree.init($("#treeDemo"), setting, zNodesNew);
		}else{
			$("#treeDemo").append("找不到数据！");
		}
	};
	ds.on('dataload',function(){
		buildZTree(ds.root);
	});
 	ds.select();
 	$("#groupSerBtn").on('click',function(){
 		buildGridView();
 	});
 	$("#groupCodeId").on('keydown',function(e){
 		if(e.keyCode == 13){
 			buildGridView();
 		}
 	});

	function buildGridView(){
		var metaGroupsql = "SELECT t.XMLID,t.TEAM_CODE,t.TEAM_NAME,t.REMARK,(select count(*) from metadb_list_permission where team_code = t.team_code)DBCNT FROM meta_team t";
		var serchText = $("#groupCodeId").val();
		if(serchText!=null && serchText != undefined && serchText.trim()!=''){
			metaGroupsql="select k.* from ("+metaGroupsql+") k where k.TEAM_CODE like '%"+serchText+"%' or k.TEAM_NAME like '%"+serchText+"%'";
		}		
		var metaTeamStore = new AI.JsonStore({
			sql:metaGroupsql,
	        pageSize:10
	    });
		$("#gridsumList").empty();
		function _rowClickFunc(index, rowdata){
			currentTeamCode = rowdata.get('TEAM_CODE');
			ds.select(teamPermission.replace('{TEAM_CODE}',rowdata.get('TEAM_CODE')));
		};
		var config={
			region:'center',
             store:metaTeamStore,
             pageSize:10,
             containerId:'gridsumList',
             nowrap:false,
             showcheck:false,
             rowclick:_rowClickFunc,
             columns:[
                    {"header":"团队ID", dataIndex:"TEAM_CODE"},
                    {"header":"团队名称", dataIndex:"TEAM_NAME"},
                    {"header":"数据库数量", dataIndex:"DBCNT"},
                    {"header":"团队说明", dataIndex:"REMARK"},
             	 ]
		} ;
        var grid =new AI.Grid(config);
		currentTeamCode = metaTeamStore.getAt(0).get('TEAM_CODE');
        ds.select(teamPermission.replace('{TEAM_CODE}',currentTeamCode));
     };
     buildGridView();
	
	$("#pullright").on('click',function() {
		var delSql="delete from METADB_LIST_PERMISSION WHERE TEAM_CODE='"+currentTeamCode+"'";
		ai.executeSQL(delSql);
		var sql ="select XMLID,TEAM_CODE,DB_XMLID,DBNAME from METADB_LIST_PERMISSION ";
		var store= new AI.JsonStore({
			sql:sql,
			pageSize:-1,
			table:'METADB_LIST_PERMISSION',
			key:'DBNAME,TEAM_CODE',
			dataSource:'METADB'
		});
			   	
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		nodes = zTree.getCheckedNodes();
		for (var i=0, l=nodes.length; i<l; i++) {
			var record=store.getNewRecord();
			record.set("TEAM_CODE",currentTeamCode);
			record.set('DB_XMLID',nodes[i].id);
			record.set("DBNAME",nodes[i].id);
			record.set('xmlid',ai.guid());
			store.add(record);
		}
		var rs = store.commit(true);
		var rsJson = $.parseJSON(rs);
		if(rsJson.msg){
			alert(rsJson.msg);
		}else{
			if(rsJson)
				alert("成功更新数据"); 
			else
				alert("更新数据失败");
		}
	});
	         
	function setCheck() {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		py = $("#py").attr("checked")? "p":"",
		sy = $("#sy").attr("checked")? "s":"",
		pn = $("#pn").attr("checked")? "p":"",
		sn = $("#sn").attr("checked")? "s":"",
		type = { "Y":py + sy, "N":pn + sn};
		zTree.setting.check.chkboxType = type;
		//showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
	};
	function showCode(str) {
		if (!code) code = $("#code");
		code.empty();
		code.append("<li>"+str+"</li>");
	}
	
	$(".table").removeClass("table-striped");
});
	</SCRIPT>
</HEAD>

<BODY>

 <div>
	<div class="col-sm-6">
		<section class="panel panel-info">
			<header class="panel-heading">
				<span style="float: left">数据目录团队权限配置</span>
			</header>
			<nav id="useroperate" class="navbar navbar-default" role="navigation" >
				<form class="navbar-form navbar-left">
					<div class="form-group">
						<input type="text" class="input-sm form-control" id = "groupCodeId" placeholder ="团队ID/团队名称">
						<button type="button" class="btn btn-sm btn-info" id = "groupSerBtn">搜索</button>
					</div>
					<button id="pullright" class="btn btn-sm btn-success">保存</button>
				</form>
			</nav>
			<div id="gridsumList"></div>
		</section>
	</div>
	<div class="col-sm-6">
		<section class="panel panel-default">
			<header class="panel-heading" style="display:none">
				<div class="input-group text-sm">
					<input type="text" class="input-sm form-control" placeholder="Search" id = "modelId">
						<div class="input-group-btn">
							<button type="button" class="btn btn-sm btn-default" id = "modelSerBtn">搜索</button>
						</div>
				</div>
			</header>
			<div class="panel-body slim-scroll"><ul id="treeDemo" class="ztree"></ul></div>
		</section>
	</div>
</div>

</BODY>
</HTML>